<?php
namespace Admin\Controller;
use Think\Controller;
class QuestionController extends CommonController {
    public function index(){
    	$res=M('question t1')->join('user t2 on t1.uid=t2.id')->join('category t3 on t1.cid=t3.id')->field('t1.*, t2.nickname, t3.cname')->where("t1.qstate='0'")->select();
    	$this->assign('question',$res);
        $this->display();
    }
    //问题编辑
    public function questionedit()
    {
    	$qid = I('get.id');
    	$cate=D('Category')->GetCate();
		$this->assign('cate',$cate);
    	$res=M('question')->where("id='{$qid}'")->field('id, title, content, cid')->select();
        $res = current($res);
        $this->assign('question',$res);
    	$this->display();
    }



    // //提交编辑
    public function editsave()
    {


        //获取POST表单所有信息
        $data=I('post.');
        $qid=I('post.qid');
        //增加时间
        $data['create_time']=time();
        $data['updata']=time();
        //修改提问
        if (M('question')->where("id='{$qid}'")->save($data)) {
            $arr = array(
                'error'=>0,
                'info'=>'修改成功!',
                );
            echo json_encode($arr);
            exit;
        }else{
            $arr = array(
                'error'=>1,
                'info'=>'修改失败'
                );
            echo json_encode($arr);
            exit;
        }
    }
    //删除单条问题
    public function delquestion()
    {
        $qid=I('post.qid');
        $res=M('question')->where("id='{$qid}'")->setField('qstate','1');
        if ($res) {
            $arr = array(
                'error'=>0,
                'info'=>'删除成功!',
                );
            echo json_encode($arr);
            exit;
        }else{
            $arr = array(
                'error'=>0,
                'info'=>'删除失败!',
                );
            echo json_encode($arr);
            exit;
        }
    }
    //批量删除问题
    public function delall()
    {
        //接到字符串 去最后一位 ',' 
        $num=rtrim($_POST['num'],',');

        $res=M('question')->where("id in($num)")->setField('qstate','1');

        //执行判断
        if($res){
            $arr=array(
                'error'=>0,
                'info'=>'删除成功'
                );
            echo json_encode($arr);
            exit();
        }else{
            $arr=array(
                'error'=>1,
                'info'=>'删除失败'
                );
            echo json_encode($arr);
            exit();
        };
    }
}
?>